package com.xmlt.core;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.xmlt.core.template.contentgenerators.GeneratorRegistry;
import com.xmlt.core.template.contentgenerators.IContentGeneratorFactory;
import com.xmlt.core.template.types.IValidator;
import com.xmlt.core.template.types.IValidatorFactory;
import com.xmlt.core.template.types.ValidatorRegistry;

public class CorePlugin implements BundleActivator {

	public static final String VALIDATOR_EXTENSION_POINT_ID = "com.xmlt.core.CorePlugin.ValidatorFactory";
	public static final String GENERATOR_EXTENSION_POINT_ID = "com.xmlt.core.CorePlugin.GeneratorFactory";

	private static BundleContext context;

	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext bundleContext) throws Exception {
		CorePlugin.context = bundleContext;

		IConfigurationElement[] validatorConfigs = Platform
				.getExtensionRegistry().getConfigurationElementsFor(
						VALIDATOR_EXTENSION_POINT_ID);
		for (IConfigurationElement e : validatorConfigs) {
			String id = e.getAttribute("id");
			ValidatorRegistry.addValidatorFactory(id, (IValidatorFactory) e.createExecutableExtension("class"));
		}

		IConfigurationElement[] generatorConfigs = Platform
				.getExtensionRegistry().getConfigurationElementsFor(
						GENERATOR_EXTENSION_POINT_ID);
		for (IConfigurationElement e : generatorConfigs) {
			String id = e.getAttribute("id");
			GeneratorRegistry.addGeneratorFactory(id, (IContentGeneratorFactory) e.createExecutableExtension("class"));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		CorePlugin.context = null;
	}

}
